#encoding:utf-8
from scrapy.spiders import CrawlSpider,Rule
from scrapy.linkextractors import LinkExtractor
from scrapy import optional_features
optional_features.remove('boto')
from meizitu import items

class MeizituSpider(CrawlSpider):
    name='meizituSpider'
    allowed_domains=['meizitu.com']
    start_urls=['http://www.meizitu.com/a/list_1_1.html',]
    def process_value(value):
        print value
    rules=[
        Rule(
            LinkExtractor(
                allow=(''),
                deny=(),
                allow_domains=(),
                deny_domains=(),
                deny_extensions=None,
                restrict_xpaths=('//div[@id="wp_page_numbers"]/ul/li'),
                restrict_css=(),
                tags=('a',),
                attrs=('href',),
                canonicalize=True,
                unique=True,
                process_value=None
            ),
            follow=True,
        ),
        Rule(
            LinkExtractor(
                allow=(),
                deny=(),
                allow_domains=(),
                deny_domains=(),
                deny_extensions=None,
                restrict_xpaths=('//h3[@class="tit"]'),
                restrict_css=(),
                tags=('a',),
                attrs=('href',),
                canonicalize=True,
                unique=True,
            ),
            callback='parse_tag',
            follow=False,
        ),
    ]

    def parse_tag(self,response):
        self.logger.info(response.url)
        item=items.MeizituItem()
        item['groupUrl']=response.url
        item['tags']=response.xpath('//div[@class="metaRight"]/p/text()').extract()
        item['title']=response.xpath('//div[@class="metaRight"]/h2/a/text()').extract()
        pics=response.xpath('//div[@id="picture"]/p/img')
        for pic in pics:
            item['imgUrl']=pic.xpath('@src').extract()
            yield item







